﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Text.RegularExpressions;
using System.Security;
using IceTea.Resources;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using IceTea.Domain.Models;

namespace IceTea.Domain
{
    /// <summary>
    /// User Member class 
    /// </summary>
    public class IceTeaMembership : IValidatableObject
    {
        public IceTeaMembership()
        { }

        public bool RetrieveUser()
        {
            User = null;

            using(IceTeaEntities ite = new IceTeaEntities())
            {
                //retrieve the User by username or email
                Regex regex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                User user = null;
                if (regex.IsMatch(LoginName))
                {
                    user = (from u in ite.Users where u.Email == LoginName select u).FirstOrDefault();
                }
                else
                {
                    user = (from u in ite.Users where u.UserName == LoginName select u).FirstOrDefault();
                }
                
                User = user;
            }

            return User != null;
        }

        protected User User {get; set;}

        [Display(Name="Login Name")]
        [Required(ErrorMessage="Please enter the login name")]
        [DisplayFormat(ConvertEmptyStringToNull=false)]
        [DataType(DataType.Text)]
        public string LoginName { get; set; }

        [Display(Name="Password")]
        [DataType(DataType.Password)]
        [Required(ErrorMessage="Please enter your password")]
        [DisplayFormat(ConvertEmptyStringToNull=false)]
        [StringLength(50, MinimumLength = 6, ErrorMessage = "Your password length must not less than 6")]
        public string LoginPass { get; set; }

        public string UserName
        {
            get
            {
                if (User != null)
                    return User.UserName;
                return null;
            }
        }

        #region Member ship operations
        
        public bool ValidatePassword()
        {
            if (SecurityHelper.HashPassword(LoginPass).Equals(User.Password))
                return true;

            return false;
        }
        #endregion

        #region Implements of IValidatableObject
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            List<ValidationResult> errors = new List<ValidationResult>();

            if (!RetrieveUser())
            {
                errors.Add(new ValidationResult("Login user name doesn't exist"));
                return errors;
            }

            if (!ValidatePassword())
            {
                errors.Add(new ValidationResult("Password is incorrect"));
                return errors;
            }

            return errors;
        }
        #endregion

    }
}
